//
//  AXFSearchViewController.m
//  AXFEightGroup
//
//  Created by ooops on 12/26/16.
//  Copyright © 2016 ya. All rights reserved.
//
/**
 - (void)setupDataOne {
 self.titleArray = @[@"中国",@"韩国",@"日本",@"越南jkjkjkjafjkjf圣诞节分时间段越南jkjkjkjafjkjf圣诞节分时间段越南jkjkjkjafjkjf圣诞节分时间段",@"泰国水电费水电费",@"巴基斯坦",@"叙利亚",@"智利",@"马来西亚",@"卡塔尔",@"菲律宾",@"新加坡",@"伊朗",@"朝鲜"];
 NSLog(@"title = %@",self.titleArray);
 [self.view addSubview:self.searchView];
 }
 */
#import "AXFSearchViewController.h"
#import "AXFDataModel.h"
#import "AXFSearchFlowLayout.h"
#import "WaterFlowCollectionViewCell.h"
#import "AXFDetailViewController.h"
#import "AXFCollentionViewHeaderView.h"
#import "AXFSearchTipsView.h"

@interface AXFSearchViewController () <UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)AXFAutoresizeLabelFlow *searchView;
@property (nonatomic, strong) UICollectionView *collectionView;      /** 搜索结果显示 */
@property (nonatomic, strong) NSArray<NSString *> *titleArray;       /** 数组 */
@property (nonatomic, strong) NSArray *searchResult;                 /** 搜索结果视图 */
@property (nonatomic, strong) UISearchBar *searchBar;                /** 搜索条 */
@property (nonatomic, strong) AXFSearchTipsView *searchTipsView;     /** 搜索条 */
@end

@implementation AXFSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor sj_colorWithHex:0xEDEDED];
    [self setupHotSearchLabel];
    //[self searchNavigation];
    [self setupSearchBar];
    [self hotSearchList];
    [self setupData];
}

#pragma mark - 加载collectionview控件显示热门搜索数据
- (void)hotSearchList {
    _searchView = [[AXFAutoresizeLabelFlow alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 100) titles:self.titleArray selectedHandler:^(NSUInteger index, NSString *title) {
        //搜索操作
        //NSLog(@"%@ - 搜索结果",title);
        [SVProgressHUD show];
        [self searchWithHotKey:title];
    }];
    [self.view addSubview:self.searchView];
}

#pragma mark - 搜索方法
- (void)searchWithHotKey:(NSString *)hotKey {
    [_ModelMgr promotions:^(NSArray<AXFDataModel *> * _Nullable models) {
        self.searchResult = models;
        [self setupCollectionView];
        _searchView.hidden = NO;
        [_searchBar setText:hotKey];
        [self.searchTipsView setSearchResultLabelText:hotKey];
        //配置搜索结果视图
        [_collectionView reloadData];
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - 加载数据
- (void)setupData {
    [_ModelMgr searchs:^(NSArray<AXFDataModel *> * _Nullable models) {
        self.titleArray = [models[0] hotquery];
        self.searchView.titles = self.titleArray;
    }];
}

#pragma mark - 导航相关设置
- (void)searchNavigation {
    self.navigationController.navigationBar.barTintColor = [UIColor sj_colorWithHex:0xCBCBCB];
}

#pragma mark - 搜索条
- (void)setupSearchBar {
    //导航条的搜索条
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0f,0.0f,SCREEN_WIDTH - 80,44.0f)];
    [[[self.searchBar.subviews.firstObject subviews] firstObject] removeFromSuperview];
    self.searchBar.delegate = self;
    [self.searchBar setPlaceholder:@"搜索"];
    
    UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 44)];
    searchView.backgroundColor = [UIColor clearColor];
    [searchView addSubview:self.searchBar];
    
    self.navigationItem.titleView = searchView;
}

#pragma mark - 热门搜索标签
- (void)setupHotSearchLabel {
    [_searchView insertLabelWithTitle:@"热门搜索" atIndex:0 animated:YES];
}

#pragma mark - 热搜词搜索结果显示collectionview
- (void)setupCollectionView {
    AXFSearchFlowLayout *waterFlowLayout = [AXFSearchFlowLayout new];
    //waterFlowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 30);
    //waterFlowLayout.sectionHeadersPinToVisibleBounds = YES;
    CGRect rect = CGRectMake(0, navBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT);
    _collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:waterFlowLayout];
    _collectionView.contentInset = UIEdgeInsetsMake(0, 0, ScrollViewHeight, 0);
    _collectionView.backgroundColor = [UIColor sj_randomColor];//self.view.backgroundColor;
    _collectionView.delegate = self;
    _collectionView.dataSource  = self;
    
    [_collectionView addSubview:self.searchTipsView];
    
    [_collectionView registerNib:[UINib nibWithNibName:@"WaterFlowCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WaterFlowCollectionViewCell"];
    //[_collectionView registerNib:[UINib nibWithNibName:@"AXFCollentionViewHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"AXFCollentionViewHeaderView"];
    //self.view = _collectionView;
    [self.view addSubview:_collectionView];
}

#pragma mark - 顶部tip视图

- (AXFSearchTipsView *)searchTipsView {
    if(_searchTipsView == nil) {
        _searchTipsView = nibView(@"AXFSearchTipsView");
        _searchTipsView.frame = CGRectMake(0, 0, SCREEN_WIDTH, TipViewHeight);
    }
    return _searchTipsView;
}

#pragma mark - UICollectionView DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.searchResult.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AXFDataModel *tempModel = self.searchResult[indexPath.item];
    WaterFlowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WaterFlowCollectionViewCell" forIndexPath:indexPath];
    cell.model = tempModel;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AXFDetailViewController *detailView = [AXFDetailViewController new];
    if (self.navigationController) {
        detailView.model = self.searchResult[indexPath.item];
        [self.navigationController pushViewController:detailView animated:YES];
    }
}

//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//    
//    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"AXFCollentionViewHeaderView" forIndexPath:indexPath];
//        return headerView;
//    }
//    return nil;
//}

#pragma mark - searchBar代理方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if(searchText.length == 0) {
        [searchBar setPlaceholder:@"搜索"];
        self.collectionView.hidden = YES;
        self.searchView.hidden = NO;
    } else {
        [self searchWithHotKey:searchText];
    }
}



//- (UIButton *)navigationBarLeftButtonFactory {
//    // 返回按钮内容左靠
//    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    
//    // 让返回按钮内容继续向左边偏移10
//    button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
//    
//    //viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
//}


@end
